package com.eportal.action.background;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.domain.FeeAlipayAuth;
import com.eportal.domain.Sysopt;
import com.eportal.domain.TOpenLog;
import com.eportal.service.FeeAlipayAuthService;
import com.eportal.util.CityUtil;
import com.eportal.util.HomePageConfig;
import com.framework.jqgrid.JqGridPager;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 支付宝授权管理
 * @author chencheng
 *
 */
public class FeeAlipayAuthAction extends BaseAction implements ModelDriven<FeeAlipayAuth> {
	
	private static Logger log = Logger.getLogger(FeeAlipayAuthAction.class.getName());
	
    /**
	 * @author chencheng
	 */
	private static final long serialVersionUID = 1L;

    private Map<String, Object> result = new HashMap<String, Object>();
    
    @Autowired
    FeeAlipayAuthService feeAlipayAuthService;
    
    /**
     * 支付宝授权管理
     */
    public String listView() {
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	getRequest().setAttribute("userRole", userRole);
    	String homePageConfig = HomePageConfig.ZHIFUBAO;
    	String[] str = homePageConfig.split(",");
		getRequest().setAttribute("mpid", Integer.parseInt(str[0]));
    	getSession().setAttribute("mpid", Integer.parseInt(str[0]));
        return SUCCESS;
    }
    
    /**
     * 支付宝授权管理列表
     */
    public void page() throws Exception {
        JqGridPager jqGridPager = new JqGridPager();
    	String agtCode = getRequest().getParameter("agtCode");
    	
    	
    	Sysopt  user= (Sysopt)getSession().getAttribute("user");
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	StringBuilder where = new StringBuilder(" 1=1 ");
    	if (userRole == 1 || userRole == 2 || userRole == 21) {
    		if (StringUtils.isNotBlank(agtCode)) {
                where.append(" and t.agtCode = '");
                where.append(agtCode);
                where.append("'");
            }
    		if (userRole == 21) {
    			where.append(" and a.sysoptId =");
    			where.append(user.getSid());
			}
		}else	if (userRole == 3 || userRole == 4) {
			where.append(" and t.agtCode ='");
			where.append(user.getAgtCode());
			where.append("'");
		}else if (userRole == 5 || userRole == 6) {
    		return;
    	}
    	jqGridPager.setWhere(where.toString());
        setPage(getRequest(), jqGridPager);
        if(jqGridPager.getSidx()==null||"".equals(jqGridPager.getSidx())){
    		jqGridPager.setSidx("t.sid");
    		jqGridPager.setSord("desc");
    	}
        List<Map<String, Object>> fixlist = feeAlipayAuthService.list(jqGridPager);
        jqGridQueryMap(TOpenLog.class, getRequest(), fixlist, getResponse(), jqGridPager);
    }
    
   
    /**
     * 支付宝授权新增
     */
    public String addView() {
        return SUCCESS;
    }
    /**
     * 支付宝重新授权
     */
    public String editView() {
    	FeeAlipayAuth feeAlipayAuth = feeAlipayAuthService.getFeeAlipayAuth(getModel().getSid());
    	getRequest().setAttribute("feeAlipayAuth", feeAlipayAuth);
    	return SUCCESS;
    }
    
    /**
     * 支付宝授权新增保存
     */
    public void add(){
    	FeeAlipayAuth feeAlipayAuth = getModel();
    	result = feeAlipayAuthService.addMethod(feeAlipayAuth);
        responseBody(result);
    }
    
    /**
     * 支付宝授权回调页面
     */
    public String feeAlipayAuthView() {
    	try{
    		String app_auth_code = getRequest().getParameter("app_auth_code");//授权码
        	String app_id = getRequest().getParameter("app_id");
        	String sid = getRequest().getParameter("sid");
        	if (StringUtils.isBlank(app_id) || StringUtils.isBlank(app_auth_code)) {
        		getRequest().setAttribute("code", 0);
    		}else{
    			//使用app_auth_code换取app_auth_token
    			result = feeAlipayAuthService.codeChangeTokenMethod(sid,app_id,app_auth_code);
    		}
        	log.info(result.get("msg"));
        	if(!"1".equals(result.get("code").toString())){
        		
        	}else{
        		result.put("code", 1);
            	result.put("msg", "获取支付宝授权信息成功");	
        	}
        	getRequest().setAttribute("result", result);
    	}catch(Exception e){
    		log.error(this, e);
    	}
    	return SUCCESS;
    }
    
    /**
     * 支付宝授权修改保存
     */
    public void update(){
    	FeeAlipayAuth feeAlipayAuth = getModel();
    	result = feeAlipayAuthService.updateMethod(feeAlipayAuth);
        responseBody(result);
    }
    
    /**
     * 支付宝授权删除
     */
    public void delete(){
    	String ids = getRequest().getParameter("ids");
    	result = feeAlipayAuthService.deleteMethod(ids);
        responseBody(result);
    }
    
    
    /**
     * 支付宝授权详情
     */
    public String applyDetailView() {
    	StringBuilder sql = new StringBuilder("select t.intro,to_char(t.apply_start_time,'yyyy-mm-dd') as starttime,to_char(t.apply_end_time,'yyyy-mm-dd') as endtime,t.pic  from FIX_APPLY t where t.sid=");
		sql.append(getRequest().getParameter("sid"));
    	Map<String, String> replyDetailMap = feeAlipayAuthService.getDetailBySql(sql.toString());
        getRequest().setAttribute("map", replyDetailMap);
        return SUCCESS;
    }
    

    private FeeAlipayAuth feeAlipayAuth = new FeeAlipayAuth();

    @Override
    public FeeAlipayAuth getModel() {
        return feeAlipayAuth;
    }
}
